package cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing;

import cn.edu.neu.softlab633.influencemaximization.sy.Index.NetworkIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.Index.TopicIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing.InfluenceCal.InfluenceCal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jason on 2017/5/6.
 */
public class FacebookData implements Preprocessing {
    @Override
    public void topicPreprocessing() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("data/FT/facebook_username.csv"));
        BufferedReader twitter_br = new BufferedReader(new FileReader("data/FT/twitter_topic_1.csv"));
        BufferedReader alignment_br = new BufferedReader(new FileReader("data/FT/alignment.csv"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/FT/facebook_topic_1.csv"));
        Map<Integer, String> twitter_topic = new HashMap<>();
        Map<Integer, Integer> alignment = new HashMap<>();
        String line = twitter_br.readLine();
        while (line != null) {
            int id = Integer.valueOf(line.split(",")[0]);
            String topic = line.split(",")[1];
            twitter_topic.put(id, topic);
            line = twitter_br.readLine();
        }
        line = alignment_br.readLine();
        while (line != null) {
            int fid = Integer.valueOf(line.split(",")[0]);
            int tid = Integer.valueOf(line.split(",")[1]);
            alignment.put(fid, tid);
            line = alignment_br.readLine();
        }
        line = br.readLine();
        while (line != null) {
            int id = Integer.valueOf(line.split(",")[0]);
            StringBuilder topic = new StringBuilder();
            if (alignment.containsKey(id)) {
                topic.append(twitter_topic.get(alignment.get(id)));
            } else {
                for (int i = 0; i < ConstantTopic.topicNum; i++) {
                    topic.append(Math.random() + " ");
                }
            }
            bw.write(id + "," + topic);
            bw.newLine();
            line = br.readLine();
        }
        br.close();
        twitter_br.close();
        alignment_br.close();
        bw.close();
    }

    @Override
    public void relationPreprocessing() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("data/FT/facebook_network.csv"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/FT/facebook_network_topic_1.csv"));
        long start = System.currentTimeMillis();
        Map<Integer, Double[]> usertopic = TopicIndex.getTopicIndex("data/FT/facebook_topic_1.csv");
        long end = System.currentTimeMillis();
        System.out.println("建立Facebook topic索引耗时：" + (end - start));
        start = System.currentTimeMillis();
        Map<Integer, ArrayList<Integer>> idf = NetworkIndex.getReverseIndex("data/FT/facebook_network.csv");
        end = System.currentTimeMillis();
        System.out.println("建立Facebook network索引耗时：" + (end -start));
        String line = br.readLine();
        line = br.readLine();
        start = System.currentTimeMillis();
        while (line != null) {
            int id1 = Integer.valueOf(line.split(",")[0]);
            int id2 = Integer.valueOf(line.split(",")[1]);
//            String influence = InfluenceCal.calEdgeWeight(id1, id2, usertopic, idf);
//            bw.write(id1 + "," + id2 + "," + influence);
            bw.newLine();
            line = br.readLine();
        }
        end = System.currentTimeMillis();
        System.out.println("生成文件耗时：" + (end - start));
        br.close();
        bw.close();
    }
}
